package com.yqzl.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yqzl.entity.*;
import com.yqzl.service.CommentService;
import com.yqzl.service.T_BlogService;
import com.yqzl.service.T_LabelService;
import com.yqzl.service.User_infoService;
import com.yqzl.util.DataUtils;
import com.yqzl.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

@RestController
@Slf4j
public class BlogController {
  @Resource
  T_BlogService t_blogService;

  @Resource
  T_LabelService t_labelService;

  @Resource
  User_infoService user_infoService;

  @Resource
  CommentService commentService;

  @PostMapping("/releaseBlog")
  //  发布博客
  public Result releaseBlog(@RequestBody Blog blog,
                            @SessionAttribute("user") User user) {
    String blogUUID = UUID.randomUUID().toString();
    String labelUUID = UUID.randomUUID().toString();

    blog.setBid(blogUUID);//博客id，uuid生成
//     因为登录才可以发送博客，所以一定会有session
    if (!ObjectUtils.isEmpty(user)) {
      blog.setUidauthor(user.getUid());
    }

//     文章要和标签 uid 关联
    blog.setLid(labelUUID);

    blog.setCreatetime(DataUtils.getCurrentChinaTimeString());

    t_blogService.save(blog);
//    都设置同一个 lid
    //    所有的标签要关联文章
    String[] labels = blog.getLabel();
//    labels 元素为0 不会进入循环
    for (String label : labels) {
      Label newLabel = new Label(null, labelUUID, label);
      t_labelService.save(newLabel);
    }


//    bid	varchar	博客id，uuid生成
//    uid_author	varchar	作者uuid  服务器有
//    lid	varchar	博客所属标签 uuid id
//    bid_origin	varchar	文章转发自，原文的id 这个字段就不用了


    System.out.println(blog);
    return Result.success().setMsg("发布文章成功");
  }

  @RequestMapping("/bLogList")
  //   返回所有的博客 信息 ，
  public Result bLogList() {
    List<Blog> bLogList = t_blogService.list();
    System.out.println(bLogList);
//     获取标签id
    for (Blog blog : bLogList) {
      log.info("博客={}", blog);
//       查询info表 自己的名字映射上
      Userinfo userinfoByUid = user_infoService.getUserinfoByUid(blog.getUidauthor());
      log.info("zz={}", userinfoByUid);
      if (!ObjectUtils.isEmpty(userinfoByUid)) {
        blog.setAuthorName(userinfoByUid.getNickname());
      }

      QueryWrapper<Label> objectQueryWrapper = new QueryWrapper<>();
      String lid = blog.getLid();

      objectQueryWrapper.eq("lid", lid);

//     获取 文章的所有标签
      List<Label> list = t_labelService.list(objectQueryWrapper);

//  设置文章的所有标签
      blog.setLabel(DataUtils.toStringArraylist(list));
      log.info("label={}", list);
    }
    return Result.success().setData(bLogList);
  }

  @RequestMapping("/blogDetail/{bid}")
//   根据博客id 返回 博客信息
  public Result getBlogInfo(@PathVariable("bid") String bid) {
    Blog blogByBid = t_blogService.getBlogByBid(bid);
    Userinfo userinfoByUid = user_infoService.getUserinfoByUid(blogByBid.getUidauthor());
    blogByBid.setAuthorName(userinfoByUid.getNickname());

    return Result.success(blogByBid);
  }

  //评论
  @PostMapping("/comment")
  public Result getComment(@RequestBody Comment comment, @SessionAttribute("user") User user) {
    if (ObjectUtils.isEmpty(user)) {
      return Result.fail("没有登录·，不能评论");
    }
    if (!StringUtils.hasText(comment.getMsg())) {
      return Result.fail("评论内容不能为null");
    }
    //contentType: application/json
    comment.setUidCmmting(user.getUid());
    log.info("comment={}", comment);
    commentService.save(comment);
    comment.setUser(user);
    return Result.success().setMsg("评论成功！").setData(comment);
  }

  //  不携带携带bid  报错 "status": 400,
//    "error": "Bad Request",
  @GetMapping("/getComments")
//  @RequestParam 必须携带bid 默认是true
  public Result getCommentByBlogId(@RequestParam("bid") String bid) {
    List<Comment> allCommentByBId = commentService.getAllCommentByBId(bid);
    return Result.success(allCommentByBId).setMsg("博客文章对应的所有的评论");
  }

  //  根据用户返回所有的博客
  @GetMapping("/getBlogbyuid")
  public Result getBlogByUid(@RequestParam("uid") String uid) {
    List<Blog> blogs = t_blogService.getBlogByUid(uid);
    return Result.success(blogs).setMsg(uid + "发布所有的博客信息");
  }

  //   根据博客bid 删除博客
  @GetMapping("/delBlogbybid/{bid}")
  public Result delBlogbyuid(@PathVariable("bid") String bid) {
    System.out.println(bid);
    t_blogService.delBlogByBid(bid);
    return Result.success().setMsg("删除成功");
  }

  //   根据博客bid 编辑博客
  @PutMapping("/updateBlogbybid")
  public Result updateBlogbybid(@RequestBody Blog blog) {
    t_blogService.upBlogByBid(blog);
    return Result.success().setMsg("修改成功");
  }
  //   根据博客bid 返回评论数


}
